package com.zxbit.flash.base;

import android.annotation.SuppressLint;

import com.zxbit.flash.network.ApiService;
import com.zxbit.flash.network.CustomConsumer;
import com.zxbit.flash.network.RetrofitHelper;
import com.zxbit.flash.response.BlankResponse;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;

/**
 * Created by zhanglibo on 2018/1/20.
 */

@SuppressLint("CheckResult")
public class BasePresenter {
    protected RetrofitHelper helper;
    protected Retrofit mRetrofit;
    protected ApiService req;

    public BasePresenter() {
        helper = RetrofitHelper.getRetorfitHelper();
        mRetrofit = helper.getRetrofitClient();
        req = mRetrofit.create(ApiService.class);
    }

    public <T extends BaseResponse> void toSubScribe(Observable<T> o, CustomConsumer<T> c, Consumer<Throwable> e) {
        o.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(c, e);
    }

    public <T> void toSubScribe(List<Observable<? extends Object>> o, Consumer<BlankResponse> c, Consumer<Throwable> e) {
        Observable.zip(o, new Function<Object[], BlankResponse>() {
            @Override
            public BlankResponse apply(Object[] objects) throws Exception {
                return null;
            }
        }).subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(c, e);


    }


}
